package com.lwq.lwqmicoservice.gateway.util;

import com.lwq.lwqmicoservice.common.model.SysPermission;
import com.lwq.lwqmicoservice.framework.annotation.PermissionController;
import com.lwq.lwqmicoservice.framework.annotation.PermissionInterface;
import org.reflections.Reflections;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Description:
 * Author: LiuWenQing
 * Datetime: 2021/12/2 15:46
 */
public class AnnotationManager {

    /**
     * controller包下 包的名,以逗号隔开
     */
    private static String  packageList = "";


    /**
     * 获取指定文件下面的RequestMapping方法保存在mapp中
     *
     * @return
     */
    public static List<SysPermission> getPermissionByAnno() {
//        if(StringUtils.isEmpty(AnnotationManager.packageList)){
//            return null;
//        }
        Set<Class<?>> classesList = new HashSet<>();
        for (String p: AnnotationManager.packageList.split(",")){
            Reflections reflections = new Reflections("com.lwq.lwqmicoservice.gateway.controller"+p);
            classesList.addAll(reflections.getTypesAnnotatedWith(PermissionController.class));
        }
        List<SysPermission> permissionList = new ArrayList<>();
        for (Class classes : classesList) {
            PermissionController permissionController = (PermissionController) classes.getAnnotation(PermissionController.class);
            Method[] methods = classes.getDeclaredMethods();
            for (Method method : methods) {
                PermissionInterface permissionInterface = method.getAnnotation(PermissionInterface.class);
                if (null != permissionInterface) {
                    SysPermission sysPermission = new SysPermission();
                    sysPermission.setCode(permissionInterface.code());
                    sysPermission.setName(permissionInterface.name());
                    sysPermission.setType(permissionController.type());
                    permissionList.add(sysPermission);
                }
            }
        }
        return permissionList;
    }

}
